<?php

/*
 * 仓库管理
 * @author wyb
 */

namespace Seller\Controller;

class WarehouseController extends BaseController {

    private $_model = null;

    public function _initialize() {
        parent::_initialize();
        $this->_model = D('customer_warehouse');
    }

    /**
     * 仓库列表
     */
    public function lists() {
        $lists = $this->_model->where(array('customer_id' => $this->__seller_info['id']))->field('id,name,address,create_time,(SELECT COUNT(id) AS total FROM `zt_goods_supply` WHERE customer_id = zt_customer_warehouse.customer_id) AS total')->select();
        $total = $this->_model->where(array('customer_id' => $this->__seller_info['id']))->count();
        $this->__assignPage($total);
        $this->assign('lists', $lists);
        $this->display();
    }

    /**
     * 仓库添加
     */
    public function add() {
        if (IS_POST) {
            isset($this->__post['name']) && $this->__post['name'] ? '' : $this->error('信息错误');
            isset($this->__post['address']) && $this->__post['address'] ? '' : $this->error('信息错误');
            isset($this->__post['city_id']) && $this->__post['city_id'] ? '' : $this->error('信息错误');
             $data = $this->__post;
            if(count($data['storage_overdue_rule'])<3) $this->error('信息错误');
            if($data['storage_overdue_rule']['id'] == 3){
                $data['storage_overdue_rule']['weight'] = $data['storage_overdue_rule']['weight']*1000000;
            }else if($data['storage_overdue_rule']['id'] == 2){
                $data['storage_overdue_rule']['weight'] = $data['storage_overdue_rule']['weight']*1000;
            }
            $data['storage_overdue_rule'] =  serialize($data['storage_overdue_rule']);
             $data['time'] = time();
            $data ['customer_id'] = $this->__seller_info['id'];
            if ($this->_model->add($data)) {
                $this->success('仓库添加成功',U('lists'));
            } else {
                $this->error('仓库添加失败');
            }
        }
        $this->assign('address',M('area')->where(array('parentid' => 0))->select());
        $this->display('form');
    }

    /**
     * 仓库修改
     */
    public function edit() {
        if (IS_POST) {
            isset($this->__post['id']) && intval($this->__post['id']) > 0 ? '' : $this->error('信息错误');
            isset($this->__post['name']) && $this->__post['name'] ? '' : $this->error('信息错误');
            isset($this->__post['address']) && $this->__post['address'] ? '' : $this->error('信息错误');
            isset($this->__post['city_id']) && $this->__post['city_id'] ? '' : $this->error('信息错误');
            $data = $this->__post;
            if(count($data['storage_overdue_rule'])<3) $this->error('信息错误');
            if($data['storage_overdue_rule']['id'] == 3){
                $data['storage_overdue_rule']['weight'] = $data['storage_overdue_rule']['weight']*1000000;
            }else if($data['storage_overdue_rule']['id'] == 2){
                $data['storage_overdue_rule']['weight'] = $data['storage_overdue_rule']['weight']*1000;
            }
            $data['storage_overdue_rule'] =  serialize($data['storage_overdue_rule']);
            $data ['customer_id'] = $this->__seller_info['id'];
            if ($this->_model->where(array('customer_id'=> $this->__seller_info['id'],'id'=>$this->__post['id']))->save($data)) {
                $this->success('仓库更新成功',U('lists'));
            } else {
                $this->error('仓库更新失败');
            }
        }
        if (!isset($this->__get['id']) || intval($this->__get['id']) <= 0)
            exit('信息不存在');
        $info = $this->_model->where(array('id' => $this->__get['id'], 'customer_id' => $this->__seller_info['id']))->find();
        if (!$info)
            $this->error('信息不存在');
        $info['storage_overdue_rule'] = unserialize($info['storage_overdue_rule']);
         if($info['storage_overdue_rule']['id'] == 3){
                $info['storage_overdue_rule']['weight'] = $info['storage_overdue_rule']['weight']/1000000;
            }else if($info['storage_overdue_rule']['id'] == 2){
                $info['storage_overdue_rule']['weight'] = $info['storage_overdue_rule']['weight']/1000;
            }
         $this->assign('address',M('area')->where(array('parentid' => 0))->select());
        $this->assign('info', $info);
        $this->display('form');
    }

    /**
     * 仓库删除
     */
    public function del() {
        if (!isset($this->__get['id']) || intval($this->__get['id']) <= 0)
            exit('信息不存在');
        if( $this->_model->where(array('id' => $this->__get['id'], 'customer_id' => $this->__seller_info['id']))->delete() ) {
            $this->error('仓库删除成功',U('lists'));
        } else {
            $this->error('删除失败');
        }
    }

}
